package cn.wolfcode.wolf2w.search.service.impl;


import cn.wolfcode.wolf2w.search.api.domain.DestinationEs;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.StringUtils;
import cn.wolfcode.wolf2w.destination.api.RemoteDestinationService;
import cn.wolfcode.wolf2w.destination.api.domain.Destination;
import cn.wolfcode.wolf2w.search.query.SearchQueryObject;
import cn.wolfcode.wolf2w.search.repository.DestinationEsRepository;
import cn.wolfcode.wolf2w.search.service.IDestinationEsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DestinationEsServiceImpl implements IDestinationEsService {

    @Autowired
    private DestinationEsRepository repository;

    @Autowired
    private RemoteDestinationService remoteDestinationService;

    @Override
    public void save(DestinationEs destination) {
        repository.save(destination);
    }

    @Override
    public void update(DestinationEs destination) {
        repository.save(destination);
    }

    @Override
    public DestinationEs get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<DestinationEs> list() {
        List<DestinationEs> list = new ArrayList<>();
        Iterable<DestinationEs> all = repository.findAll();
        all.forEach(list::add);
        return list;
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Page<Destination> queryByKeyWord(SearchQueryObject qo) {
        String keyword = qo.getKeyword();
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());

        SearchPage<DestinationEs> searchPage = repository.queryByNameOrInfo(keyword, keyword, pageRequest);

        List<Destination> list = searchPage.getSearchHits().stream().map(searchHit -> {
            R<Destination> ret = remoteDestinationService.getOne(Long.parseLong(searchHit.getId()), SecurityConstants.INNER);
            StringUtils.handleESHighlight(searchHit.getHighlightFields(), ret.getData());
            return ret.getData();
        }).collect(Collectors.toList());

        return new PageImpl<>(list, pageRequest, searchPage.getTotalElements());
    }

    @Override
    public void initDestinationEs() {
        R<List<Destination>> result = remoteDestinationService.list(SecurityConstants.INNER);
        List<Destination> list = result.getData();
        List<DestinationEs> esList = list.stream().map(record -> {
            DestinationEs mddEs = new DestinationEs();
            BeanUtils.copyProperties(record, mddEs);
            return mddEs;
        }).collect(Collectors.toList());

        repository.saveAll(esList);
    }
}
